from java.lang import Throwable
all,failed=0,0



def parse(*packages):
    """
    introspect a list of packages and test some java.lang.Class methods
    """
    global all, failed
    for package in iter(packages):
        #get all objects from the module
        #print "parsing", package
        children =[]
        for name in dir(package):
            try:
                all+=1
                children.append(getattr(package, name))
            except:
                failed+=1
                #print "failed loading java class ", name, " Skipped"  
        
        #filter java packages
        java_packages = [p 
                        for p in children  
                        if type(p).__name__=="javapackage" ] 
        #for each package
        for p in java_packages:
            if p!=package: #avoid infinite loop
                parse(p)  #recursively parse packages
    
        #then for this package gets all the classes
        java_classes = [clazz 
                        for clazz in children  
                        if isinstance(clazz, java.lang.Class) ]
        
        #and for each class
        for c in java_classes:
            all+=1
            try:
                unittest(c)
                """for m in c.methods:
                    for t in m.parameterTypes:
                        unittest(t)"""
            except Exception, e:
                #print "failed ",c, "due to ", e  #why them ? java.lang.Class, java.net.MulticastSocket
                failed+=1
    

def unittest(c):
    c.name
    c.interface
    c.desiredAssertionStatus()
    c.getAnnotations()
    c.getCanonicalName()
    c.getClasses()
    c.getClassLoader()
    c.getComponentType()
    c.getConstructors()
    c.getDeclaredAnnotations()
    c.getDeclaredClasses()
    c.getDeclaredConstructors()
    c.getDeclaredFields()
    c.getDeclaredMethods()
    c.getDeclaringClass()
    c.getEnclosingClass()
    c.getEnclosingConstructor()
    c.getEnclosingMethod()
    c.getEnumConstants()
    c.getFields()
    c.getGenericInterfaces()
    c.getGenericSuperclass()
    c.getInterfaces()
    c.getMethods()
    c.getModifiers()
    c.getName()
    c.getPackage()
    c.getProtectionDomain()
    c.getSigners()
    c.getSimpleName()
    c.getSuperclass()
    c.getTypeParameters()
    c.isAnnotation()
    c.isAnonymousClass()
    c.isArray()
    c.isEnum()
    c.isInterface()
    c.isLocalClass()
    c.isMemberClass()
    c.isPrimitive()
    c.isSynthetic()
    c.toString()

def test_packages(*packages):
    global all,failed
    all,failed=0,0
    parse(*packages)
    print str([p.__name__ for p in packages])+" failed %s over %s, at rate %f"%(failed, all, (failed*1./all) )


import  java, javax, org, com
test_packages(java)
test_packages(javax)
test_packages(com)
test_packages(java, javax,  com)
"""
['java'] failed 1276 over 2661, at rate 0.479519
['javax'] failed 1664 over 3569, at rate 0.466237
['com'] failed 4188 over 9386, at rate 0.446196
['java', 'javax', 'com'] failed 7128 over 15616, at rate 0.456455
"""
    